package com.zhou.common.operator.log.core.util;


import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

@Slf4j
public class IpUtil {
    public static final String LOCAL_IPV4_ADDRESS = "127.0.0.1";
    public static final String LOCAL_IPV6_ADDRESS = "0:0:0:0:0:0:0:1";

    public static String parseClientIp(HttpServletRequest request) {
        if (request == null) {
            return "unknown";
        } else {
            String ip = request.getHeader("x-forwarded-for");
            if (!isIpValid(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }

            if (!isIpValid(ip)) {
                ip = request.getHeader("X-Forwarded-For");
            }

            if (!isIpValid(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }

            if (!isIpValid(ip)) {
                ip = request.getHeader("X-Real-IP");
            }

            if (!isIpValid(ip)) {
                ip = request.getRemoteAddr();
            }

            return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
        }
    }

    private static boolean isIpValid(String ip) {
        return StringUtils.isNotBlank(ip) && !"unknown".equalsIgnoreCase(ip);
    }

    public static String localIp() {
//        List<String> ipList = getLocalIpList();
//        return !ipList.isEmpty() ? (String) ipList.get(0) : "127.0.0.1";
        InetAddress inetAddress = null;
        try {
            inetAddress = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            log.error("", e);
        }
        if (inetAddress != null) {
            return inetAddress.getHostAddress();
        }
        return "127.0.0.1";
    }

    public static List<String> getLocalIpList() {
        ArrayList ipList = new ArrayList();
        try {
            Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
            InetAddress ip = null;

            while (true) {
                NetworkInterface netInterface;
                do {
                    do {
                        if (!allNetInterfaces.hasMoreElements()) {
                            return ipList;
                        }

                        netInterface = allNetInterfaces.nextElement();
                    } while (netInterface.isVirtual());
                } while (!netInterface.isUp());

                Enumeration addresses = netInterface.getInetAddresses();

                while (addresses.hasMoreElements()) {
                    ip = (InetAddress) addresses.nextElement();
                    if (ip != null && ip instanceof Inet4Address) {
                        ipList.add(ip.getHostAddress());
                    }
                }
            }
        } catch (Exception var5) {
            var5.printStackTrace();
            return ipList;
        }
    }

    public static void main(String[] args) throws UnknownHostException {
        long start = System.currentTimeMillis();
        System.out.println(localIp());
        log.info("耗时={}", System.currentTimeMillis() - start);
        start = System.currentTimeMillis();
        InetAddress inetAddress = InetAddress.getLocalHost();
        String ipAddress = inetAddress.getHostAddress();
        log.info("ipAddress={}", ipAddress);
        log.info("耗时={}", System.currentTimeMillis() - start);


    }
}

